package io.renren.modules.sell.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.QiniuCloudUtil;
import io.renren.common.utils.R;
import io.renren.modules.sell.entity.ProductCategory;
import io.renren.modules.sell.entity.ProductInfo;
import io.renren.modules.sell.service.ProductService;
import io.renren.modules.sys.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName ProductController
 * @Author mengjiafeng
 * @Date 2020/4/12 17:17
 **/
@RestController
@RequestMapping("seller/product")
public class ProductController extends AbstractController {
    @Autowired
    ProductService productService;

    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("createUserId", getUserId());
        PageUtils page = productService.queryPageByUserId(params);
        return R.ok().put("page", page);
    }
    @GetMapping("/info/{productId}")
    public R getCategory(@PathVariable("productId") Long productId){
        ProductInfo productInfo = productService.getOne(new QueryWrapper<ProductInfo>().eq("product_id",productId));
        return R.ok().put("productInfo",productInfo);

    }

    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        UUID uuid = UUID.randomUUID();
        File file = new File("C:\\test");
        multipartFile.transferTo(file);
        String upload = QiniuCloudUtil.upload(file, uuid.toString());
        return R.ok().put("file",upload);

    }


    @PostMapping("/save")
    @Transactional
    public R save(@RequestBody ProductInfo productInfo){

        productInfo.setSellerId(getUserId());
        productInfo.setCreateTime(new Date());
        productInfo.setUpdateTime(new Date());
        productService.save(productInfo);
        return R.ok();
    }

    @PostMapping("/update")
    @Transactional
    public R update(@RequestBody ProductInfo productInfo){
        productInfo.setSellerId(getUserId());
        productInfo.setUpdateTime(new Date());
        productService.updateById(productInfo);

        return R.ok();
    }

    //批量删除订单
    @PostMapping("/delete")
    public R listByUserId(@RequestBody Integer[] productIds){
        productService.deleteBatch(productIds);
        return R.ok();
    }
}
